package com.ddlaw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ddlaw.entity.TLawKnowledge;
import com.ddlaw.entity.TLawKnowledgeDetail;
import com.ddlaw.entity.TLawKnowledgeType;
import com.ddlaw.service.LawKnowledgeService;

@Controller
@RequestMapping(value="knowledge")
public class KonwledgeController extends BaseController{
	
	@Resource
	LawKnowledgeService knowService;
	
	
	/**
	 * 法律知识首页
	 * @param model		模型数据
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String knowledge(Model model){
		Map<String,Object> params = new HashMap<String,Object>();
		Map<TLawKnowledge,List<TLawKnowledgeType>> groups = new HashMap<TLawKnowledge,List<TLawKnowledgeType>>();
		List<TLawKnowledge>  knowledges= knowService.getKnowledge(params);
		for(TLawKnowledge k:knowledges){
				params.put("knowledge", k.getLawknowid());
				List<TLawKnowledgeType> knowTypes  = knowService.getMinKnowledge(params);
				groups.put(k, knowTypes);
		}
		model.addAttribute("knowledge", groups);
		return "knowledge/index";
	}
	
	/**
	 * 法律知识列表
	 * @param model		模型数据
	 * @param type		知识大类型
	 * @param s				知识小类型
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Model model,
			@RequestParam(value="type",defaultValue="0") Integer type,
			@RequestParam(value="s",defaultValue="0") Integer s){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("knowledge", type);
		params.put("s", s);
		if(type != 0){
			List<TLawKnowledgeType> knowTypes  = knowService.getMinKnowledge(params);
			model.addAttribute("knowTypes", knowTypes);
			model.addAttribute("show", 1);
			model.addAttribute("s", s);
		}
		List<TLawKnowledgeDetail> details = knowService.getDetailById(params);
		model.addAttribute("typeId", type);
		model.addAttribute("details", details);
		return "knowledge/list";
	}

	@RequestMapping("detail/{detailId}")
	public String detail(@PathVariable("detailId") Integer detailId,Model model){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("detailId", detailId);
		List<TLawKnowledgeDetail> details = knowService.getDetailById(params);
		knowService.addReadNum(detailId);
		model.addAttribute("details", details);
		return "knowledge/detail";
	}
}
